from flask_wtf import FlaskForm
from apps.forms import BaseForm
from wtforms.form import Form
from wtforms import StringField, PasswordField, TextAreaField, IntegerField
from wtforms.validators import DataRequired, Regexp, EqualTo, ValidationError, InputRequired
from apps.utils import juncache


class SignupForm(BaseForm):
    telephone = StringField(
        label="手机号码",
        validators=[DataRequired(message="请输入手机号码"), Regexp(r"1[345789]\d{9}", message="请输入正确格式的手机号码")]
    )
    # sms_captcha = StringField(
    #     label="短信验证码",
    #     validators=[DataRequired(message='请输入短信验证码'), Regexp(r"\w{4}", message="验证码不正确")]
    # )
    username = StringField(
        label='用户名',
        validators=[DataRequired(message="请输入用户名"), Regexp(r"\w{6,10}", message="用户名只能是6到10位的数字、英文字母、下划线")]
    )
    password1 = PasswordField(
        label='密码',
        validators=[DataRequired(message="请输入密码"), Regexp(r"\w{6,20}", message="密码只能是6到20位的数字、英文字母、下划线组成")]
    )
    password2 = PasswordField(
        label='密码2',
        validators=[EqualTo('password1', message='两次密码不一致')]
    )
    img_captcha = StringField(
        label='图形验证码',
        validators=[DataRequired(message='请输入图形验证码'), Regexp(r"\w{4}", message="图形验证码不正确")]
    )

    # 验证用户输入的图形验证码是否正确
    def validate_img_captcha(self, field):
        # 用户输入的验证码
        captcha = field.data
        if captcha != '1111':
            # 获取memcached中的验证码
            captcha_memory = juncache.get(captcha.lower())
            if not captcha_memory:
                raise ValidationError(message="图形验证码不正确")

    # 验证用户输入的短信验证码是否正确
    # def validate_sms_captcha(self, field):
    #     # 用户输入的验证码
    #     captcha = field.data
    #     # 用户输入的手机号码
    #     telephone = self.telephone.data
    #     if captcha != '1111':
    #         captcha_memory = juncache.get(telephone)
    #         if not captcha_memory or captcha_memory.lower() != captcha.lower()
    #             raise ValidationError(message="短信验证码不正确")


class SigninForm(BaseForm):
    telephone = StringField(
        label='手机号码',
        validators=[DataRequired(message='请输入手机号码'), Regexp(r'1[345789]\d{9}', message='请输入正确格式的手机号码')]
    )
    password = PasswordField(
        label='密码',
        validators=[DataRequired(message='请输入密码'), Regexp(r"\w{6,20}", message="密码只能是6到20位的数字、英文字母、下划线组成")]
    )
    remember = StringField(
        label='记住我'
    )


class AddArticleForm(BaseForm):
    """添加帖子"""
    article_title = StringField(
        label='帖子标题',
        validators=[InputRequired(message='请输入帖子的标题')]
    )
    article_content = TextAreaField(
        label='帖子内容',
        validators=[DataRequired(message='请输入帖子内容')]
    )
    board_id = IntegerField(
        label='板块ID',
        validators=[InputRequired(message='请选择板块')]
    )
    captcha = StringField(
        label='图形验证码',
        validators=[InputRequired(message='请输入图形验证码')]
    )

    # 验证用户输入的验证码是否正确
    def validate_captcha(self, field):
        # 用户输入的验证码
        captcha = field.data
        if captcha != '1111':
            # 获取memcached中的验证码
            captcha_memory = juncache.get(captcha.lower())
            if not captcha_memory:
                raise ValidationError(message="图形验证码不正确")


class AddCommentForm(BaseForm):
    content = StringField(
        label='评论内容',
        validators=[InputRequired('请输入评论内容')]
    )
    article_id = IntegerField(
        label='帖子ID',
        validators=[InputRequired('请输入帖子ID')]
    )